#!/usr/bin/env python
# coding: utf-8

# Copyright (c) 2020 CSTCHC Technologies Co., Ltd.
# oec-hardware is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Create: 2020-04-01

"""Operation of client """

import os
import sys
import fcntl
import argparse

sys.path.append("/usr/share/cstchc/lib/")
os.putenv("PYTHONPATH", "/usr/share/cstchc/lib/")

from cstchccompatible.compatibility import EulerCertification
import cstchccompatible.version


class CertLock:
    """
    certlock
    """
    def __init__(self, filename):
        self.filename = filename
        self.fd_obj = open(filename, 'w')

    def acquire(self):
        """
        acquire
        """
        try:
            fcntl.flock(self.fd_obj, fcntl.LOCK_EX|fcntl.LOCK_NB)
            return True
        except IOError:
            return False

    def release(self):
        """
        release
        """
        fcntl.flock(self.fd_obj, fcntl.LOCK_UN)

    def __del__(self):
        self.fd_obj.close()


if __name__ == '__main__':
    if os.getuid() > 0:
        sys.stderr.write("You need to be root to run this program.\n")
        sys.exit(1)

    parser = argparse.ArgumentParser(description="Run CSTC-hardwareCap Compatibility Test Suite")
    parser.add_argument('--clean', action='store_true',
                        help='Clean saved testsuite.')
    parser.add_argument('--rebootup', action='store_true',
                        help='Continue run testsuite after reboot system.')
    parser.add_argument('--version', action='store_true',
                        help='Show testsuite version.')
    args = parser.parse_args()

    lock = CertLock("/var/lock/cstchc.lock")
    if not lock.acquire():
        sys.stderr.write("The cstchc may be running already, you should not run it repeated.\n")
        sys.exit(1)

    cert = EulerCertification()
    if args.clean:
        if not cert.clean():
            lock.release()
            sys.exit(1)
    elif args.rebootup:
        if not cert.run_rebootup():
            lock.release()
            sys.exit(1)
    elif args.version:
        print("version: %s" % cstchccompatible.version.version)
    else:
        if not cert.run():
            lock.release()
            sys.exit(1)

    lock.release()
    sys.exit(0)
